package net.qunzhi.modules.cms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.qunzhi.common.exception.RRException;
import net.qunzhi.common.utils.BaseQuery;
import net.qunzhi.common.annotation.SysLog;
import net.qunzhi.common.utils.ShiroUtils;
import net.qunzhi.common.validator.ValidatorUtils;
import net.qunzhi.modules.app.query.CopyDataQuery;
import net.qunzhi.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.qunzhi.modules.cms.entity.TbCopyFlowDataEntity;
import net.qunzhi.modules.cms.service.TbCopyFlowDataService;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.text.html.parser.Entity;


/**
 * 手抄的流量计数据
 *
 * @author zjr
 * @date 2021-09-10 15:48:00
 */
@Api(tags = "手抄的流量计数据相关")
@RestController
@RequestMapping("cms/tbCopyFlowData")
public class TbCopyFlowDataController {
    @Autowired
    private TbCopyFlowDataService tbCopyFlowDataService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    @RequiresPermissions("cms:tbCopyFlowData:list")
    public R list(CopyDataQuery query){
        PageUtils page = tbCopyFlowDataService.queryPage(query);

        return R.ok().put("page", page);
    }
 

    /**
     * 信息
     */
    @ApiOperation("信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("cms:tbCopyFlowData:info")
    public R info(@PathVariable("id") Long id){
		TbCopyFlowDataEntity tbCopyFlowData = tbCopyFlowDataService.getById(id);

        return R.ok().put("tbCopyFlowData", tbCopyFlowData);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @SysLog("手抄的流量计数据保存")
    @PostMapping("/save")
    @RequiresPermissions("cms:tbCopyFlowData:save")
    public R save(@RequestBody TbCopyFlowDataEntity tbCopyFlowData){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        tbCopyFlowData.setCreateUserId(userEntity.getUserId()).setCreateUserName(userEntity.getUsername());
        tbCopyFlowDataService.save(tbCopyFlowData);

        return R.ok();
    }

    @ApiOperation("批量保存")
    @SysLog("手抄的流量计数据保存")
    @PostMapping("/batchsave")
    @RequiresPermissions("cms:tbCopyFlowData:save")
    public R save(@RequestBody List<TbCopyFlowDataEntity> form){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        List<TbCopyFlowDataEntity> list = form.stream().filter(entity -> null != entity.getPositiveFlow()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            throw new RRException("请输入正流量行度");
        }
        list.forEach(entity -> {
                    entity.setCreateUserId(userEntity.getUserId()).setCreateUserName(userEntity.getUsername());
                });
        tbCopyFlowDataService.saveBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @SysLog("手抄的流量计数据修改")
    @PostMapping("/update")
    @RequiresPermissions("cms:tbCopyFlowData:update")
    public R update(@RequestBody TbCopyFlowDataEntity tbCopyFlowData){
		tbCopyFlowDataService.updateById(tbCopyFlowData);

        return R.ok();
    }

    @ApiOperation("导入")
    @SysLog("导入")
    @PostMapping("/importExcel")
    @RequiresPermissions("cms:tbCopyFlowData:importExcel")
    public R importExcel(String fileName, MultipartFile file) throws Exception {
        tbCopyFlowDataService.importExcel(fileName,file);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @SysLog("手抄的流量计数据删除")
    @PostMapping("/delete")
    @RequiresPermissions("cms:tbCopyFlowData:delete")
    public R delete(@RequestBody Long[] ids){
		tbCopyFlowDataService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
